package com.hss01248.facedetect.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.hss01248.facedetect.R;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@SuppressWarnings("All")
public class FaceDetectActivity extends BaseActivity implements Callback, PreviewCallback {


    //@Bind(R.id.sv_preview_detect)
    SurfaceView svPreview;
   // @Bind(R.id.iv_image_detect)
    ImageView ivImage;

    private Camera mCamera;

    private int width = 640;
    private int height = 480;

    private boolean flag = true;

    private Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            Bitmap rotateBitmap = getRotateBitmap(bitmap, -90);

            ivImage.setImageBitmap(rotateBitmap);
            camera.startPreview();
            dealWithFaceDetect(bitmap2Bytes(rotateBitmap));
        }
    };

    private byte[] bitmap2Bytes(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    private void dealWithFaceDetect(final byte[] data) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_detect);
        svPreview = (SurfaceView) findViewById(R.id.sv_preview_detect);
        ivImage = (ImageView) findViewById(R.id.iv_image_detect);
       // ButterKnife.bind(this);

      //  initToolBar(true, toolbar, R.string.title_detect);
        initData();
    }

    private void initData() {
        svPreview.getHolder().addCallback(this);
        svPreview.setKeepScreenOn(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera = Camera.open(1);
            Camera.Parameters para = mCamera.getParameters();
            para.setPreviewSize(width, height);
            mCamera.setParameters(para);
            mCamera.setDisplayOrientation(90);
        }catch (Exception e){

            Toast.makeText(this, "异常 camera "+e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCamera.takePicture(null, null, pictureCallback);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
            mCamera.setPreviewCallback(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    @Override
    public void onPreviewFrame(final byte[] data, final Camera camera) {
        Bitmap bitmap = null;
        if (data != null) {

        }

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public static Bitmap getRotateBitmap(Bitmap bitmap, float rotateDegree){
        Matrix matrix = new Matrix();
        matrix.postRotate((float) rotateDegree);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    }

}
